// pages/city/city.js
var that,
  api = new(require('../../utils/api.js')),
  headimg = api.getimgurl(),
  QQMapWX = require('../../utils/qqmap-wx-jssdk.js'),
qqmapsdk,
  PinYin = require('../../utils/pinyin.js'),
  util = new(require('../../utils/util.js'));
const app = getApp()

function isChinese(str) {
  var entryVal = str;
  var cnChar = entryVal.match(/[^\x00-\x80]/g);
  if (cnChar != null && cnChar.length > 0)
    return true;
  return false;
}

function arraySearch(str) {
  for (var name in PinYin) {
    if (PinYin[name].indexOf(str) != -1) {
      return name;
      break;
    }
  }
  return false;
}


function getPinYinFirstCharacter(str, split,uppercase) {
  split = split || " ";
  uppercase = uppercase || false;
  var len = str.length;
  var result = "";
  var reg = new RegExp('[a-zA-Z0-9\- ]');
  var val;
  var name;
  for (var i = 0; i < len; i++) {
    val = str.substr(i, 1);
    if (isChinese(val)) {
      name = arraySearch(val);
      if (reg.test(val)) {
        result += split + val;
      } else if (name !== false) {
        result += split + name.substring(0, 1);
      }
    } else {
      result +=val;
    }
  }
  if(uppercase) result = result.toUpperCase();
  result = result.replace(split,"");
  return result.trim();
}
Page({

  /**
   * 页面的初始数据
   */
  data: {
    list:[],
    citylist:[],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    that=this;
    // let res =  getPinYinFirstCharacter('北上天杭',',',!0).split(',')
    // console.log(res,'res');
    api.getcitylist(res=>{
      if(res.data.issuccess==1){
        that.setData({list:res.data.list},()=>{
          that.getinitials()
        })
      }
    })
  },
  getinitials(){
    let {list}=that.data,initials="",initialslist=[],citylist=[]
    list.forEach((x,i)=>{
      initials+=x.name["0"]
      // console.log(initials,'initials');
    })
     let res =  getPinYinFirstCharacter(initials,',',!0).split(',')
    // console.log(res,'res');
    res.forEach((x,i)=>{
      // citylist.forEach((y,j)=>{
        if(citylist.length==0){
          let item={"initials":x,"city":[list[i]]}
          citylist.push(item)
          // console.log(citylist,'citylist');
        }else{
          citylist.forEach((y,j)=>{
            initialslist.push(y.initials)
          })
          if(initialslist.indexOf(x)==-1){
            let item={"initials":x,"city":[list[i]]}
            citylist.push(item)
          }else{
            citylist.forEach((y,j)=>{
              if(y.initials==x){
                citylist[j].city.push(list[i])
              }
            })
          }
        }
      // })
    })
    // console.log(citylist,'citylistcitylist');
    that.setData({citylist})
  },
  // 点击切换城市
  citysel(e){
    let {city}=e.currentTarget.dataset
    wx.setStorageSync("city",city.name)
    wx.switchTab({
      url: '../index/index'
    });
    console.log(e,'eeee');
    
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})